//
//  BJTabbarViewController.m
//  BaoJunNewEnergy
//
//  Created by 9s on 2018/3/15.
//  Copyright © 2018年 SIXECO. All rights reserved.
//

#import "BJTabbarViewController.h"
#define tempTag 20000000

#define TabBarTitles [NSArray arrayWithObjects:@"发现",@"商城",@"车服务",@"好玩",@"我的",nil]

#define TabBarPics [NSArray arrayWithObjects:@"find",@"mall",@"carService",@"fun",@"mine",nil]
@implementation RootTabBarItem

-(instancetype)initWithFrame:(CGRect)frame image:(NSString *)imageName
{
    self=[super initWithFrame:frame];
    if (self) {
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.titleLabel.font=[UIFont systemFontOfSize:12];
        self.titleLabel.textAlignment=NSTextAlignmentCenter;
        [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:[imageName stringByAppendingString:@"_s"]] forState:UIControlStateSelected];
        [self setTitleColor:RGB(0, 0, 0) forState:UIControlStateNormal];
        [self setTitleColor:RGB(32, 200, 167) forState:UIControlStateSelected];
    }
    return self;
}

-(void)setHighlighted:(BOOL)highlighted
{
    
}

//设置图片位置
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    return CGRectMake((contentRect.size.width-25)/2,3,25,30);
}
//设置文字位置
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(0,29,contentRect.size.width,20);
}

@end


@interface BJTabbarViewController ()<UITabBarControllerDelegate>
@property(nonatomic,strong)UIView * rootTabBar;
@end

@implementation BJTabbarViewController
{
    RootTabBarItem * _lastItem;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTabBar];
    [self setControllers];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(turnIndex:) name:@"kTurnIndex" object:nil];
    // Do any additional setup after loading the view.
}
-(void)createTabBar
{
    //CGRect tabBarFrame=self.tabBar.frame;
    [self.tabBar removeFromSuperview];
    
    if (IOS11) {
        self.tabBar.hidden = YES;
    }
    _rootTabBar=[[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-TabbarHeight, ScreenWidth, TabbarHeight)];
    _rootTabBar.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_rootTabBar];
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = _rootTabBar.bounds;
    [_rootTabBar addSubview:effectView];
    
    UIView* line=[[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-TabbarHeight, ScreenWidth, 0.5)];
    line.backgroundColor=RGBA(223, 223, 223, 0.7);
    [_rootTabBar addSubview:line];
    
    [TabBarTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RootTabBarItem * item=[[RootTabBarItem alloc] initWithFrame:CGRectMake(ScreenWidth/TabBarTitles.count*idx, 0, ScreenWidth/TabBarTitles.count, 49)
                                                              image:TabBarPics[idx]];
        [item setTitle:(NSString *)obj forState:UIControlStateNormal];
        [item addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        if (idx==0) {
            item.selected=YES;
            _lastItem=item;
        }
        item.tag=tempTag+idx;
        [_rootTabBar addSubview:item];
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (IOS11) {
        
    }
}
-(void)removeTabBarButton {
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            child.hidden = YES;
            child.userInteractionEnabled = NO;
        }
    }
}
-(void)setControllers
{
    
}
-(void)setHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed
{
    if (self.tabBar) {
        [self.tabBar removeFromSuperview];
    }
    //重写tabBar底部
    if (hidesBottomBarWhenPushed)
    {
        if (_rootTabBar.frame.origin.y==ScreenHeight-TabbarHeight) {
            [UIView animateWithDuration:0.35 animations:^{
                _rootTabBar.frame=CGRectMake(0,ScreenHeight+_rootTabBar.frame.size.height,_rootTabBar.frame.size.width, _rootTabBar.frame.size.height);
            }];
        }
    }
    else
    {
        if (_rootTabBar.frame.origin.y==ScreenHeight+TabbarHeight)
        {
            [UIView animateWithDuration:0.35 animations:^{
                _rootTabBar.frame=CGRectMake(0, ScreenHeight-TabbarHeight, _rootTabBar.frame.size.width,_rootTabBar.frame.size.height);
            }];
        }
    }
}

-(void)btnPressed:(RootTabBarItem * )item
{
    _lastItem.selected=NO;
    item.selected=YES;
    _lastItem=item;
    self.selectedIndex=item.tag-tempTag;
}

-(void)turnIndex:(NSNotification*)noti
{
    if ([noti.object isKindOfClass:[NSNumber class]]){
        _lastItem.selected=NO;
        RootTabBarItem*item=[_rootTabBar viewWithTag:[(NSNumber *)noti.object  integerValue]+tempTag];
        item.selected=YES;
        _lastItem=item;
        self.selectedIndex=item.tag-tempTag;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
